<template>
  <div class="body" v-loading="loading.status">
    <div class="table_content">
      <div class="table_filtrate">
        <div class="tf_item">
          <div class="tag">搜索</div>
          <el-input
            class="input_fuzzy_search input"
            size="mini"
            placeholder="ID|名字"
            v-model="tableParams.fuzzy_search"
            clearable
          >
          </el-input>
        </div>

        <div class="tf_item">
          <div class="tag">状态</div>

          <el-select
            class="input"
            v-model="tableParams.status"
            placeholder="请选择"
            size="mini"
          >
            <el-option label="全部" value="all"></el-option>
            <el-option label="正常" value="normal"></el-option>
            <el-option label="废弃" value="stop"></el-option>
          </el-select>
        </div>

        <div class="tf_item">
          <el-button @click="loadTableData" type="primary" size="mini" plain
            >搜索</el-button
          >
        </div>
      </div>

      <div class="table_operate">
        <el-button
          class="btn_add"
          type="primary"
          @click="handleToGoCreate"
          size="mini"
          >添加</el-button
        >
      </div>

      <div class="table-card">
        <el-table
          class="table"
          :data="tableData.data"
          :row-class-name="tableRowClassName"
          v-loading="loading.table"
          :border="true"
          size="mini"
          show-summary :summary-method="$_App_getSummaries"
        >
          <el-table-column prop="id" label="ID" width="60"> </el-table-column>
          <el-table-column prop="name" label="名称"> </el-table-column>
          <el-table-column prop="calculation_type" label="类型">
            <template slot-scope="scope">
              <el-tag size="mini" plain effect="plain">{{
                scope.row.calculation_type
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="cooperative_price" label="合作价格">
          </el-table-column>
          <el-table-column prop="yesterday_cost" label="昨日渠道成本">
          </el-table-column>
          <el-table-column prop="today_cost" label="今日渠道成本">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>

          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="normal"
                inactive-value="stop"
                active-color="#13ce66"
                @change="tableUpdateStatus($event, scope.row.id, 'status')"
                inactive-color="#ff4949"
              >
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link
                class="handle"
                type="primary"
                v-show="scope.row.status == 'normal'"
                @click="handleTableEdit(scope.$index, scope.row)"
                >编辑</el-link
              >
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleTableSizeChange"
          @current-change="handleTableCurrentChange"
          :current-page="currentPage4"
          :page-sizes="$pageSize"
          :page-size="tableParams.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { apiChannelListpage, apiChannelUpdateSingle } from "@/api/apiCommon";
import { paramsDeleteNull } from "@/utils/common";
export default {
  name: "Index",
  data() {
    return {
      tableParams: {
        page: 1,
        size: 15,
        fuzzy_search: "",
        status: "all",
      },
      tableData: {
        current_page: 1,
        data: [],
        last_page: 1,
        per_page: "15",
        total: 0,
      },
      currentPage4: 1,
      business_select: [],
      organization_select: [],
      fromViewCofing: {
        input_width: "120px",
        label_width: "120px",
      },
      loading: {
        dialog_create: false,
        table: false,
      },
    };
  },
  created() {
    this.loadTableData();
  },
  methods: {
    // 列表请求
    loadTableData(is_initpage = true) {
      const _this = this;
      if (is_initpage) {
        _this.tableParams.page = 1;
      }
      _this.loading_start("table");
      var tableParamsData = { ..._this.tableParams };
      if (tableParamsData["fuzzy_search"] == "") {
        delete tableParamsData["fuzzy_search"];
      }

      apiChannelListpage(tableParamsData)
        .then((response) => {
          const result = response;
          _this.tableData = result.data.datalist;
          _this.loading_end("table");
        })
        .catch(function (result) {
          _this.$message.error(result.msg);
          _this.tableData.data = null;
        });
    },
    // 页面跳转
    handleToGoCreate() {
      this.$router.push({ path: "/channel/create", query: { id: 0 } });
    },
    // 表格行状态
    tableRowClassName({ row, rowIndex }) {
      if (row["status"] == "stop") {
        return "discard-row";
      }
    },
    // 表格 充值/退款
    handleTableRecharge(index, row, transaction_type = "recharge") {
      var name = row.name;
      var path = "transaction";
      // if (transaction_type == "expense") {
      //   path = "expense";
      // }
      this.$router.push({
        path: path,
        query: {
          id: row.id,
          transaction_type: transaction_type,
          name: name,
        },
      });
    },
    // 表格操作
    handleTableEdit(index, row) {
      this.$router.push({ path: "/channel/update", query: { id: row.id } });
    },
    // 表格恢复
    handleTableRegain(index, row) {
      var _this = this;
      var params = {
        id: row.id,
        status: "normal",
      };
      _this.loading_start("table");
      apiProductUpdatePart(params).then((response) => {
        _this.loading_end("table");
        _this.loadTableData();
      });
    },
    // 表格删除
    handleTableDelete(index, row) {
      var _this = this;
      this.$confirm("是否更改运行状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var params = {
            id: id,
          };
          params["status"] = "stop";
          _this.loading_start("table");
          apiChannelUpdateSingle(params).then((response) => {
            _this.loading_end("table");
          });
        })
        .catch(() => {
          _this.loading_end("table");
        });
    },
    // 数量改变
    handleTableSizeChange(val) {
      this.tableParams.size = val;
      this.loadTableData(false);
    },
    // 页数改变
    handleTableCurrentChange(val) {
      this.tableParams.page = val;
      this.loadTableData(false);
      // console.log(`当前页: ${val}`);
    },
    //更新表格某行的状态
    tableUpdateStatus($event, id, key) {
      console.log([$event, id, key]);
      var _this = this;

      var params = {
        id: id,
      };

      this.$confirm("是否更改运行状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var params = {
            id: id,
          };
          params[key] = $event;
          _this.loading_start("table");
          apiChannelUpdateSingle(params).then((response) => {
            _this.loading_end("table");
          });
        })
        .catch(() => {
          _this.loadTableData();
        });
    },
    // 部分状态改变
    tableUpdateItem(item) {
      var _this = this;
      var params = {
        id: item.id,
        status: item.status,
      };
      _this.loading_start("table");
      apiChannelUpdateSingle(params).then((response) => {
        _this.loading_end("table");
      });
    },
    // 加载开始...
    loading_start(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = true;
          setTimeout(() => {
            this.loading[key] = false;
          }, 5000);
        });
      } else {
        this.loading[keys] = true;
        setTimeout(() => {
          this.loading[keys] = false;
        }, 5000);
      }
    },
    // 加载结束...
    loading_end(keys) {
      if (Array.isArray(keys)) {
        keys.forEach((key) => {
          this.loading[key] = false;
        });
      } else {
        this.loading[keys] = false;
      }
    },
  },
};
</script>

<style lang="scss"></style>
